package org.sluggard.tangela.sina;

import java.awt.Desktop;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import org.sluggard.tangela.core.Container;

import weibo4j.http.AccessToken;
import weibo4j.http.RequestToken;

public class SinaCheckBox extends JCheckBox {
	
	private RequestToken resToken = null;
	
	private SinaToken token;
	
	private JFrame frame;

	public SinaCheckBox() {
		super();
		setText("新浪微博");
		addItemListener(new SinaListener());
		token = SinaToken.getInstence();
		setSelected(token.isState());
	}

	public void getOAuthRequestToken() {
		try {
			Desktop desktop = Desktop.getDesktop();  
	        if (Desktop.isDesktopSupported() && desktop.isSupported(Desktop.Action.BROWSE)) {  
	        	URI uri = new URI(resToken.getAuthenticationURL());  
	            desktop.browse(uri);  
	        }  
		} catch (IOException ex) {  
			ex.printStackTrace();  
		} catch (URISyntaxException ex) {  
			ex.printStackTrace();  
		}  
	}
	
	private class SinaListener implements ItemListener {

		private void getAccessToken(String verifier) {
			
			if(verifier!=null)
			{
			System.out.println("oauth:"+verifier);

				if(resToken!=null)
				{
					AccessToken accessToken=OAuth.requstAccessToken(resToken,verifier);
						if(accessToken!=null) {
							//out.println(accessToken.getToken());
							//out.println(accessToken.getTokenSecret());
							//第二个参数每次只能用一次，发表微博内容不能重复，如果重复发会返回400错误
							//这个accessToken不用每次访问都重新取，可以存到session里面用
							/*
							 * 
							 * 
							 */
							
							token.setAccess(accessToken);
							System.out.println("success");
						} else {
							setSelected(false);
						}
				
				} else {
//					out.println("request token session error");
					}
			} else {
//				out.println("verifier String error");
			}

		}
		

		@Override
		public void itemStateChanged(ItemEvent e) {
			token.setState(e.getStateChange() == ItemEvent.SELECTED);
			if(e.getStateChange() == ItemEvent.DESELECTED) {
				
			} else {
				if(resToken == null) {
					resToken=OAuth.request("oob");
				}
				if(token.getAccess() == null) {
					if(frame == null) frame = new JFrame();
					new Thread(new Runnable() {
						
						@Override
						public void run() {
							int n = JOptionPane.showConfirmDialog(frame, "本地没有您的sina授权,点击是将跳转到sina网站进行授权,请将授权后的授权码输入提示框", "请确认", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
							if(n == JOptionPane.YES_OPTION) {
								getOAuthRequestToken();
								String s = (String)JOptionPane.showInputDialog(
										frame,
			                            "请输入授权码:",
			                            "Customized Dialog",
			                            JOptionPane.PLAIN_MESSAGE,
			                            null,
			                            null,
			                            "");
								getAccessToken(s.trim());
							} else {
								setSelected(false);
							}
						}
					}).start();
				} else {
					
				}
			}
			Container.addToken(token);
		}
	}
}
